Imports System.IO
Public Class FormEducation

    Dim myEducation As DataTable
    Dim i, id As Int16

    Private Sub FormEducation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.btnAdd.Enabled = App.IsAuthorized(Permissions.Manage_Education_Add)
        Me.btnUpdate.Enabled = App.IsAuthorized(Permissions.Manage_Education_Edit)
        Me.btnDelete.Enabled = App.IsAuthorized(Permissions.Manage_Education_Delete)

        UI.FormatDataGrid(dgvEducation)


    End Sub
    Private Sub initalizeData()
        Try
            loadData()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub loadData()
        'Get Data From Database
        Try
            myEducation = Educations.GetEducation
            dgvEducation.DataSource = myEducation
        Catch ex As Exception

        End Try
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        initalizeData()
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'Get last DataGridView Rows when click button add
        i = dgvEducation.Rows.Count.ToString
        i = i - 1
        dgvEducation.CurrentCell = dgvEducation.Rows(i).Cells(1)
        Dim frm As New FormEducationAED
        frm.ShowDialog()
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        With FormEducationAED

            .btnSaveUpdate.Show()
            .btnClose.Show()
        End With
        EducationUpdate()
    End Sub
    Public Sub EducationUpdate()
        'pass values from DataGridView to FormEducationAED when click Button Update
        Try


            Dim row = Me.dgvEducation.CurrentRow

            With FormEducationAED

                If row.Cells("EDUCATOIN_ID").Value Is Nothing Then
                    Exit Sub
                Else

                    .eduId = row.Cells("EDUCATOIN_ID").Value
                    .cboName.Text = row.Cells("EMPLOYEE_NAME").Value
                    .txtDuration.Text = row.Cells("DURATION").Value
                    .txtMajor.Text = row.Cells("MAJOR").Value
                    .txtCertificate.Text = row.Cells("CERTIFICATE").Value
                    .txtUniversity.Text = row.Cells("SCHOOL_NAME").Value
                    .cboCountry.Text = row.Cells("COUNTRY").Value
                    .txtNote.Text = row.Cells("NOTE").Value
                    .txtEducationGroup.Text = row.Cells("EDUCATION_GROUP").Value
                    .CboGPA.Text = row.Cells("GPA").Value
                    .txtGrateuateYear.Text = row.Cells("GRADUATE_YEAR").Value
                    .ShowDialog()
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If

        Catch ex As Exception

        End Try
        Try
            FormDelete.ShowDialog()
            If FormDelete.DialogResult = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If

            'Remove DataGridView Row and Delete data in Database
            For Each row As DataGridViewRow In dgvEducation.SelectedRows
                If row.Cells("EDUCATOIN_ID").Value = Nothing Then

                    Exit Sub

                End If
                Dim edu As New EducationInfo
                edu.EducationId = row.Cells("EDUCATOIN_ID").Value
                Educations.DeleteEducation(edu)

                Me.dgvEducation.Rows.Remove(row)
                dgvEducation.CurrentCell = dgvEducation.Rows(0).Cells(1)


            Next


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
End Class